﻿using Architecture.Repository.Impl.Traceability;
using Architecture.Repository.Interface.Traceability;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Traceability.Domain.SupplierDomain;
using Traceability.Domain.TraceabilityDto;

namespace Traceability.Domain.Service.AdditiveServices
{
    /// <summary>
    /// 添加剂服务
    /// </summary>
    public class AdditiveService : IAdditiveService
    {
        /// <summary>
        /// 包材仓储
        /// </summary>
        protected readonly IAdditivesRepository additivesRepository;

        /// <summary>
        /// 图片仓储
        /// </summary>
        protected readonly IPictureRepository pictureRepository;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="additivesRepository">包材仓储</param>
        /// <param name="pictureRepository">图片仓储</param>
        public AdditiveService(IAdditivesRepository additivesRepository, IPictureRepository pictureRepository)
        {
            this.additivesRepository = additivesRepository;
            this.pictureRepository = pictureRepository;
        }

        public async Task<int> AddAdditive(Additive additive)
        {
            return await additivesRepository.AddAsync(additive);
        }

        public async Task<int> DeleteAdditivel(int additiveId, string userMame)
        {
            var res = await additivesRepository.GetModelAsync(additiveId);
            res.DeletedBy = userMame;
            res.DeletedDate = DateTime.Now;
            res.IsDeleted = true;
            return await additivesRepository.UpdateAsync(res);
        }

        public async Task<Additive> GetAdditiveModel(int additiveId)
        {
            return await additivesRepository.GetModelAsync(additiveId);
        }

        public async Task<List<AdditiveDto>> GetAllAdditiveList(int shoppingId)
        {
            var list = (from a in await additivesRepository.GetAllAsync()
                        join b in await pictureRepository.GetAllAsync()
                        on a.AdditiveId equals b.AdditiveId
                        where a.ShoppingId == shoppingId && !a.IsDeleted && !b.IsDeleted
                        select new AdditiveDto
                        {
                            AdditiveId = a.AdditiveId,
                            AdditiveProductionBatch = a.AdditiveProductionBatch,
                            PictureId = b.PictureId,
                            PictureName = b.PictureName,
                        }).ToList();
            return list;
        }

        public async Task<int> UpdateAdditive(Additive additive)
        {
            var res = await additivesRepository.GetModelAsync(additive.AdditiveId);
            res.AdditiveId = additive.AdditiveId;
            res.AdditiveProductionBatch = additive.AdditiveProductionBatch;
            res.UpdatedBy = additive.UpdatedBy;
            res.UpdatedDate = DateTime.Now;
            return await additivesRepository.UpdateAsync(res);
        }
    }
}
